package com.jrx.anytxn.transaction.job.paph.config;

import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.job.paph.step.MergeServiceListener;
import com.jrx.anytxn.transaction.service.IPaphFileService;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 张靖烽
 * @name MergeServiceFeeConfiguration
 * @description 合并技术服务费同步分库文件批量
 * @create 2021-03-12 15:54
 **/
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class MergeServiceFeeConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private MergeServiceListener mergeServiceListener;
    @Autowired
    private IPaphFileService iPaphFileService;
    @Autowired
    private PaphFileConfig paphFileConfig;

    @Bean
    public Job mergeServiceFeeJob() {
        return jobBuilderFactory.get("mergeServiceFeeJob")
                .listener(mergeServiceListener)
                .start(mergeServiceFeeStep())
                .build();
    }

    @Bean
    public Step mergeServiceFeeStep() {
        return stepBuilderFactory.get("mergeServiceFeeStep")
                .tasklet(mergeServiceFeeTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet mergeServiceFeeTasklet() {
        return (stepContribution, chunkContext) -> {
            iPaphFileService.mergeBankFile(paphFileConfig.getServiceFeePath(), TransBizConstant.SERVICE_FEE_FILE_NAME, Constant.PRODECUT_TYPE_XF);
            iPaphFileService.mergeBankFile(paphFileConfig.getServiceFeePath(), TransBizConstant.SERVICE_FEE_FILE_NAME, Constant.PRODECUT_TYPE_JY);
            return RepeatStatus.FINISHED;
        };
    }

}
